package com.eqdd.yiqidian.ui.gongneng.renshi.gonggao;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.bigkoo.pickerview.TimePickerView;
import com.eqdd.yiqidian.CreateTrumpetActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.DateUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.Calendar;
import java.util.Date;

public class CreateTrumpetActivity extends BaseActivity {

    private CreateTrumpetActivityCustom dataBinding;
    private String title;
    private String content;
    private int num;
    Calendar now = Calendar.getInstance();


    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_create_trumpet);
    }

    @Override
    public void initData() {
        title = getIntent().getStringExtra(Config.TITLE);
        content = getIntent().getStringExtra(Config.CONTENT);
        num = getIntent().getIntExtra(Config.MAX_NUM, -1);
    }

    @Override
    public void setView() {
        initTopTitleBar(View.VISIBLE, title);
        initTopRightText("确定", v -> {
            if (!TextUtils.isEmpty(dataBinding.etContent.getText()) && !TextUtils.isEmpty(dataBinding.etTime.getText())) {

                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_TRUMPET)
                        .params("userGuid", user.getGuid())
                        .params("comid", user.getCompanyId())
                        .params("content", dataBinding.etContent.getText().toString())
                        .params("pushTime", dataBinding.etTime.getText().toString())
                        .params("lengthOfTime", TextUtils.isEmpty(dataBinding.etLong.getText()) ? "0" : dataBinding.etLong.getText().toString().replace("小时", ""))
                        .execute(new DialogCallBack<HttpResult>(CreateTrumpetActivity.this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort("请输入内容");
            }
        });

        dataBinding.etTime.setOnClickListener(v -> {
            initTime();
        });

        dataBinding.etLong.setOnClickListener(v -> {
            initLimit();
        });


        setComtextLimit();
    }

    private void setComtextLimit() {
        dataBinding.etContent.setText(content);
        if (num != 1) {
            dataBinding.tvNum.setText("0/" + num);
            dataBinding.etContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(num)});
        }
        dataBinding.etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (num != -1) {
                    dataBinding.tvNum.setText(s.length() + "/" + num);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    public void initTime() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        }
        now.setTime(new Date());
        TimePickerView time = new TimePickerView.Builder(this, (date, v) -> {
            String startDate = DateUtil.parse(date, "yyyy-MM-dd HH:mm");
            dataBinding.etTime.setText(startDate);
            now.setTime(date);
        })
                .setOutSideCancelable(true)
                .setType(new boolean[]{true, true, true, true, true, false})
                .setRange(2017, 2050)
                .setDate(now)
                .build();
        time.show();
    }

    public void initLimit() {
        String[] array = new String[]{"1小时", "2小时", "4小时", "8小时", "12小时", "24小时", "48小时"};
        ChooseAlertView.getBottomChoose(this, null, array, (o, pos) -> {
            dataBinding.etLong.setText(array[pos - 1]);
        }).show();
    }
}
